package com.ivan.stu.notetool.ImageEdit;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.ViewSwitcher;

import com.ivan.stu.notetool.R;
import com.ivan.stu.notetool.adapter.ImageAdapter;


/**
 * Created by felix on 2017/12/5 下午3:08.
 */

abstract class IMGEditBaseActivity extends Activity implements View.OnClickListener,
        IMGTextEditDialog.Callback, RadioGroup.OnCheckedChangeListener,
        DialogInterface.OnShowListener, DialogInterface.OnDismissListener ,ImageAdapter.onImageClickListener{

    protected IMGView mImgView;

    protected Bitmap mBitmap;

    protected RecyclerView mRecyclerView;

    protected ImageAdapter mAdapter;

    private RadioGroup mModeGroup;

    private IMGColorGroup mColorGroup;
    private RadioGroup mSizeGroup;

    private LinearLayout ll_pic;

    private IMGTextEditDialog mTextDialog;

    private View mLayoutOpSub;

    private ViewSwitcher mOpSwitcher, mOpSubSwitcher;

    public static final int OP_HIDE = -1;

    public static final int OP_NORMAL = 0;

    public static final int OP_CLIP = 1;

    public static final int OP_SUB_DOODLE = 0;

    public static final int OP_SUB_MOSAIC = 1;

    public static final int OP_SUB_SIZE = 2;

    public static final int OP_SUB_PIC = 3;

    protected int[] imageList = {R.mipmap.notetool_bg_add,R.mipmap.notetool_bg_white,R.mipmap.notetool_bg_white1,R.mipmap.notetool_bg_white2,R.mipmap.notetool_bg_white3};
    protected int flag = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getBitmap();

    }

    public void onCreated() {

    }

    protected void initViews() {

        mImgView = findViewById(R.id.image_canvas);
        mModeGroup = findViewById(R.id.rg_modes);
        mOpSwitcher = findViewById(R.id.vs_op);
        mOpSubSwitcher = findViewById(R.id.vs_op_sub);
        mColorGroup = findViewById(R.id.cg_colors);
        mSizeGroup = findViewById(R.id.cg_size);
        mSizeGroup.setOnCheckedChangeListener(this);
        mColorGroup.setOnCheckedChangeListener(this);
        mLayoutOpSub = findViewById(R.id.layout_op_sub);
        mLayoutOpSub.getBackground().mutate().setAlpha(240);

        ll_pic = findViewById(R.id.ll_pic);
        ll_pic.getBackground().mutate().setAlpha(50);

        mRecyclerView = findViewById(R.id.recyclerView);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);

        mRecyclerView.setLayoutManager(manager);

        mAdapter = new ImageAdapter(this,imageList);
        mAdapter.setImageClickListener(this);
        mRecyclerView.setAdapter(mAdapter);


    }


    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.rb_doodle) {   //调整画笔大小

      //      onModeClick(IMGMode.SIZE);
            onModeClick(IMGMode.SIZE);
        } else if (vid == R.id.btn_text) {  //贴纸
            onTextModeClick();
        } else if (vid == R.id.rb_mosaic) {  //马赛克
            onModeClick(IMGMode.MOSAIC);   //暂时屏蔽掉马赛克，逻辑上有问题。
        } else if (vid == R.id.btn_clip) {  //裁剪
            onModeClick(IMGMode.CLIP);
        } else if (vid == R.id.btn_undo) {  //回撤

    //        mModeGroup.clearCheck();
   //         mModeGroup.check(R.id.btn_undo);
   //         mLayoutOpSub.setVisibility(View.GONE);
   //         mSizeGroup.setVisibility(View.GONE);

            onModeClick(IMGMode.HIDE_BUT_DRAW);   //该模式会隐藏操作栏，并且禁止反选，可以进行绘画，手势放大等操作
            onUndoClick();
        } else if (vid == R.id.tv_done) {  //重做

            onRedoClick();
          //  onDoneClick();
        } else if (vid == R.id.tv_cancel) {  //取消
            onCancelClick();
        } else if (vid == R.id.ib_clip_cancel) {  //取消裁剪
            onCancelClipClick();
        } else if (vid == R.id.ib_clip_done) {  //完成裁剪
            onDoneClipClick();
        } else if (vid == R.id.tv_clip_reset) {  //还原
            onResetClipClick();
        } else if (vid == R.id.ib_clip_rotate) {  //旋转
            onRotateClipClick();
        }else if (vid == R.id.btn_color){  //调色板

            onModeClick(IMGMode.DOODLE);
        }else if (vid == R.id.bt_finish){  //完成编辑
            onDoneClick();
        }else if (vid == R.id.btn_pic){  //选择画板

            if (flag == 1){

                onModeClick(IMGMode.PIC);

            }else {

                onNotify();

                // TextViewUtils.showToast(this,1,"该模式下暂不支持切换图片功能");

                //    Toast.makeText(getApplicationContext(),"该模式下暂不支持切换图片功能",Toast.LENGTH_SHORT);
            }

        }else if (vid == R.id.btn_recover){  //下一步

            onModeClick(IMGMode.HIDE_BUT_DRAW);
            onRecoverClick();
        }
    }

    public void updateModeUI() {
        IMGMode mode = mImgView.getMode();
        switch (mode) {
            case DOODLE:
                mModeGroup.check(R.id.btn_color);
                setOpSubDisplay(OP_SUB_DOODLE);   //展示涂鸦的视图
                break;
            case MOSAIC:
                mModeGroup.check(R.id.rb_mosaic);
                setOpSubDisplay(OP_SUB_MOSAIC);  //展示马赛克的视图
                break;
            case NONE:
                mModeGroup.clearCheck();
                setOpSubDisplay(OP_HIDE);   //默认情况下视图
                break;
            case SIZE:
                mModeGroup.check(R.id.rb_doodle);
                setOpSubDisplay(OP_SUB_SIZE);   //展示涂鸦的视图
                break;
            case PIC:
                mModeGroup.check(R.id.btn_pic);
                setOpSubDisplay(OP_SUB_PIC);
                break;
            case HIDE_BUT_DRAW:
                setOpSubDisplay(OP_HIDE);
                break;

        }
    }

    public void onTextModeClick() {
        if (mTextDialog == null) {
            mTextDialog = new IMGTextEditDialog(this, this);
            mTextDialog.setOnShowListener(this);
            mTextDialog.setOnDismissListener(this);
        }
        mTextDialog.show();
    }

    @Override
    public final void onCheckedChanged(RadioGroup group, int checkedId) {

        if (group.getId() == R.id.cg_colors){

            onColorChanged(mColorGroup.getCheckColor());

        }else {

            if (mSizeGroup.getCheckedRadioButtonId() == R.id.cr_min){

                onPenSizeChanged(3, Color.WHITE);

            } else if (mSizeGroup.getCheckedRadioButtonId() == R.id.cr_min1){

                onPenSizeChanged(5,Color.WHITE);
            }else if (mSizeGroup.getCheckedRadioButtonId() == R.id.cr_min2){

                onPenSizeChanged(7,Color.WHITE);
            }else if (mSizeGroup.getCheckedRadioButtonId() == R.id.cr_min3){

                onPenSizeChanged(9,Color.WHITE);
            }else if (mSizeGroup.getCheckedRadioButtonId() == R.id.cr_min4){

                onPenSizeChanged(11,Color.WHITE);
            }else if (mSizeGroup.getCheckedRadioButtonId() == R.id.cr_min5){

                onPenSizeChanged(13,Color.WHITE);
            }

      //      onPenSizeChanged(mSizeGroup.getCheckSize(),mSizeGroup.getCheckColor());
        }
    }

    public void setOpDisplay(int op) {
        if (op >= 0) {
            mOpSwitcher.setDisplayedChild(op);
        }
    }

    public void setOpSubDisplay(int opSub) {
        if (opSub < 0) {
            ll_pic.setVisibility(View.GONE);
            mLayoutOpSub.setVisibility(View.GONE);   //隐藏工具栏布局

            mSizeGroup.setVisibility(View.GONE);
        } else if (opSub == 2){
           mOpSubSwitcher.setVisibility(View.GONE);
           mSizeGroup.setVisibility(View.VISIBLE);
           mLayoutOpSub.setVisibility(View.VISIBLE);
            ll_pic.setVisibility(View.GONE);
        }else if (opSub == 3){

            mSizeGroup.setVisibility(View.GONE);
            mOpSubSwitcher.setVisibility(View.GONE);
            ll_pic.setVisibility(View.VISIBLE);
            mLayoutOpSub.setVisibility(View.VISIBLE);

        }else {

            mSizeGroup.setVisibility(View.GONE);

            ll_pic.setVisibility(View.GONE);
            mOpSubSwitcher.setVisibility(View.VISIBLE);

            mOpSubSwitcher.setDisplayedChild(opSub);
            mLayoutOpSub.setVisibility(View.VISIBLE);  //显示工具栏布局
        }
    }

    @Override
    public void onShow(DialogInterface dialog) {
        mOpSwitcher.setVisibility(View.GONE);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mOpSwitcher.setVisibility(View.VISIBLE);
    }

    public abstract void getBitmap();

    public abstract void onModeClick(IMGMode mode);

    public abstract void onUndoClick();

    public abstract void onCancelClick();

    public abstract void onDoneClick();

    public abstract void onCancelClipClick();

    public abstract void onDoneClipClick();

    public abstract void onResetClipClick();

    public abstract void onRotateClipClick();

    public abstract void onRedoClick();

    public abstract void onColorChanged(int checkedColor);

    public abstract void onPenSizeChanged(float radius ,int color);

    public abstract void onRecoverClick();

    public abstract void onNotify();

    @Override
    public abstract void onText(IMGText text);
}
